<template>
  <div class="app-container">
    <el-card v-show="elderInfo" v-if="elderList"  style="height: 690px">
      <el-form :model="queryElder" ref="queryForm" size="mini" :inline="true" v-show="showSearch"
               label-width="68px" style="display: flex; justify-content: flex-end;">
        <el-form-item label="姓名" prop="elderName">
          <el-input
            v-model="queryElder.elderName"
            placeholder=""
            clearable
            @keyup.enter.native="handleQueryElder"
            style="width: 100px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryElder">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQueryElder">重置</el-button>
        </el-form-item>
      </el-form>


      <div style="margin-left:1%;margin-right:1%">
        <el-row>
          <el-col :span="4" v-for="(item) in elderList" :key="item.elderId" :offset="0" >
            <div class="card-container">
              <el-card :body-style="{ padding: '0px'}" shadow="hover">
                <img :src="item.elderImg" class="image" style="height: 90px;width: 150px;">
                <div class="e-info">
                  <span>姓名:{{item.elderName}}</span><br>
                  <span>性别:{{item.elderGender}}</span><br>
                  <span>房间编号:{{item.roomCode}}</span><br>
                  <div class="bottom clearfix">
                    <!--                        <time class="time"><strong>入住状态:</strong>{{item.liveStatus}}</time>-->
                    <!--                        <el-button type="danger" icon="el-icon-delete" plain class="button" @click="handleDeleteElder(item.elderId)"></el-button><span> </span>-->
                    <el-button type="success" icon="el-icon-edit" plain class="button" @click="setMedicatePaln(item)">设置计划</el-button><span></span>
                  </div>
                </div>
              </el-card>
            </div>
          </el-col>
        </el-row>
      </div>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryElder.pageNum"
        :page-sizes="[12, 24, 36, 48, 60, 72]"
        :limit.sync="queryElder.pageSize"
        @pagination="getNurseElderList"
      />
    </el-card>


    <el-card v-show="elderPlan" class="planElderList">
      <el-page-header @back="goBack" :content="eldertitle">
      </el-page-header>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" style="display: flex; justify-content: flex-end;">
        <el-form-item label="姓名" prop="elderName">
          <el-input
            v-model="queryParams.elderName"
            placeholder="请输入老人姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button
            type="danger"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="5" class="mb8">
          <el-divider content-position="left">{{ elder.elderName }}的月吃药计划<span style="color: #999; font-size: 10px;">-特殊情况可单独记录</span></el-divider>
      </el-row>

<!--      <el-row>-->
<!--        <el-col :span="3" v-for="(item) in palnList" :key="item.id" :offset="0" >-->
<!--          <div class="plan-card-container">-->
<!--            <el-card :body-style="{ padding: '0px'}" shadow="hover" class="firstCard">-->
<!--              <div class="e-info">-->
<!--                <el-card class="mealPlan" body-style="{ padding: '0px'}" shadow="hover" >-->
<!--                  <el-tag size="mini" type="success">早:</el-tag>{{item.morning}}:-->
<!--                  <el-tag  size="mini" :type="item.morningFinish == 1?'':''">{{item.morningFinish == 1?'完成':'待完成'}}</el-tag><br>-->
<!--                  <el-tag size="mini" type="warning">中:</el-tag>{{item.noon}}：-->
<!--                  <el-tag  size="mini" :type="item.noonFinish == 1?'':''">{{item.noonFinish == 1?'完成':'待完成'}}</el-tag><br>-->
<!--                  <el-tag size="mini" type="danger">晚:</el-tag>{{item.afterNoon}}：-->
<!--                  <el-tag  size="mini" :type="item.afterNoonFinish == 1?'':''">{{item.afterNoonFinish == 1?'完成':'待完成'}}</el-tag><br>-->
<!--                </el-card>-->
<!--                <div class="bottom clearfix">-->
<!--                  <time class="time">日期:<el-tag type="success" size="mini">{{item.takeTime}}</el-tag></time>-->
<!--                  <el-button type="warning" icon="el-icon-edit" plain class="button" size="mini" @click="handleUpdate(item)"></el-button>-->
<!--                </div>-->
<!--              </div>-->
<!--            </el-card>-->
<!--          </div>-->
<!--        </el-col>-->
<!--      </el-row>-->
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="吃药管理" name="first">
          <el-table v-loading="loading" :data="palnList" @selection-change="handleSelectionChange">
            <el-table-column label="姓名" align="center" prop="elderName" />
            <el-table-column label="早服用" align="center" prop="morning" />
            <el-table-column label="中服用" align="center" prop="noon" />
            <el-table-column label="晚服用" align="center" prop="afterNoon" />
            <el-table-column label="日期" align="center" prop="medicateTime" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-more"
                  @click="handleUpdate(scope.row)"
                >详情</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="吃药记录" name="second">
          <el-table v-loading="loading" :data="palnList" @selection-change="handleSelectionChange">
            <el-table-column label="姓名" align="center" prop="elderName" />
            <el-table-column label="早" align="center" prop="morning" />
            <el-table-column label="是否完成" align="center" prop="morningFinish" >
              <template slot-scope="scope">
                <el-tag v-if="scope.row.morningFinish == 1" type="success">完成</el-tag>
                <el-tag v-else-if="scope.row.morningFinish == 0" type="danger">待完成</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="中" align="center" prop="noon" />
            <el-table-column label="是否完成" align="center" prop="noonFinish" >
              <template slot-scope="scope">
                <el-tag v-if="scope.row.noonFinish == 1" type="success">完成</el-tag>
                <el-tag v-else-if="scope.row.noonFinish == 0" type="danger">待完成</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="晚" align="center" prop="afterNoon" />
            <el-table-column label="是否完成" align="center" prop="afterNoonFinish" >
              <template slot-scope="scope">
                <el-tag v-if="scope.row.afterNoonFinish == 1" type="success">完成</el-tag>
                <el-tag v-else-if="scope.row.afterNoonFinish == 0" type="danger">待完成</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="日期" align="center" prop="takeTime" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-more"
                  @click="handleUpdate(scope.row)"
                >详情</el-button>
                <!--          <el-button-->
                <!--            size="mini"-->
                <!--            type="text"-->
                <!--            icon="el-icon-delete"-->
                <!--            @click="handleDelete(scope.row)"-->
                <!--          >删除</el-button>-->
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>



      <!--      <el-table v-loading="loading" :data="palnList" @selection-change="handleSelectionChange">-->
      <!--        <el-table-column label="姓名" align="center" prop="elderName" />-->
      <!--        <el-table-column label="需服用药品" align="center" prop="drugName" />-->
      <!--        <el-table-column label="早" align="center" prop="morning" />-->
      <!--        <el-table-column label="中" align="center" prop="noon" />-->
      <!--        <el-table-column label="晚" align="center" prop="afterNoon" />-->
      <!--        <el-table-column label="是否完成" align="center" prop="isFinish" />-->
      <!--        <el-table-column label="日期" align="center" prop="takeTime" />-->
      <!--        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--          <template slot-scope="scope">-->
      <!--            <el-button-->
      <!--              size="mini"-->
      <!--              type="text"-->
      <!--              icon="el-icon-more"-->
      <!--              @click="handleUpdate(scope.row)"-->
      <!--            >详情</el-button>-->
      <!--  &lt;!&ndash;          <el-button&ndash;&gt;-->
      <!--  &lt;!&ndash;            size="mini"&ndash;&gt;-->
      <!--  &lt;!&ndash;            type="text"&ndash;&gt;-->
      <!--  &lt;!&ndash;            icon="el-icon-delete"&ndash;&gt;-->
      <!--  &lt;!&ndash;            @click="handleDelete(scope.row)"&ndash;&gt;-->
      <!--  &lt;!&ndash;          >删除</el-button>&ndash;&gt;-->
      <!--          </template>-->
      <!--        </el-table-column>-->
      <!--      </el-table>-->

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改用药计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="需服用药品" prop="drugName">-->
<!--          <el-input v-model="form.drugName" placeholder="请输入需服用药品" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="老人" prop="elderId">-->
<!--          <el-input v-model="form.elderId" placeholder="请输入老人姓名" />-->
<!--        </el-form-item>-->
        <el-form-item label="早上" prop="morning">
          <el-input v-model="form.morning" placeholder="请输入早上所需要的药品" />
        </el-form-item>
        <el-form-item label="中午" prop="noon">
          <el-input v-model="form.noon" placeholder="请输入中午所需要的药品" />
        </el-form-item>
        <el-form-item label="晚上" prop="afterNoon">
          <el-input v-model="form.afterNoon" placeholder="请输入晚上所需要的药品" />
        </el-form-item>
        <el-form-item label="日期" prop="medicateTime">
          <el-date-picker
            v-model="form.medicateTime"
            type="month"
            placeholder="选择日期"
            format="yyyy-MM"
            value-format="yyyy-MM">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openElder" width="700px" append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" >
        <el-form-item label="早上需要的用药：" prop="morning">
          {{form.morning}}: <el-tag :type="form.morningFinish=='完成'?'success':'danger'">{{form.morningFinish}}</el-tag>

        </el-form-item>
        <el-form-item label="中午需要的用药：" prop="noon">
          {{form.noon}}: <el-tag :type="form.noonFinish=='完成'?'success':'danger'">{{form.noonFinish}}</el-tag>
        </el-form-item>
        <el-form-item label="晚上需要的用药：" prop="afterNoon">
          {{ form.afterNoon }}：<el-tag :type="form.afterNoonFinish== '完成' ?'success':'danger'">{{form.afterNoonFinish}}</el-tag>

        </el-form-item>
        <el-form-item label="日期" prop="medicateTime">
         {{form.takeTime}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listPaln, getPaln, delPaln, addPaln, updatePaln, delOnePaln} from "@/api/business/plan";
  import {nurseElderList} from "@/api/business/medication";

  export default {
    name: "Paln",
    data() {
      return {
        activeName: 'first',
        elderPlan:false,
        elderInfo:true,
        // 遮罩层
        eldertitle:null,
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用药计划表格数据
        palnList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        openElder: false,
        queryElder:{
          pageNum: 1,
          pageSize: 10,
          elderName:null,
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          drugName: null,
          elderId: null,
          pointTime: null,
          isFinish: null,
          takeTime: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        elderList:{},
        elder: {},
      };
    },
    created() {
      // this.getList();
      this.getNurseElderList();
    },
    methods: {
      /** 查询用药计划列表 */
      getList(planType) {
        this.loading = true;
        this.queryParams.planType = planType;
        this.queryParams.userId = this.$store.state.user.id;
        listPaln(this.queryParams).then(response => {
          this.palnList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          drugName: null,
          elderId: null,
          pointTime: null,
          isFinish: null,
          takeTime: null,
          createTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        const planType = 1;
        this.getList(planType);
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleQueryElder() {
        this.queryParams.pageNum = 1;
        this.getNurseElderList();
      },
      /** 重置按钮操作 */
      resetQueryElder() {
        this.resetForm("queryForm");
        this.handleQueryElder();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加"+this.elder.elderName+"月用药计划";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id
        getPaln(id).then(response => {
          this.form = response.data;
          this.openElder = true;
          this.title = "用药计划详情";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.elderId = this.elder.elderId;
            if (this.form.id != null) {
              updatePaln(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPaln(this.form).then(response => {
                console.log(response.msg);
                if(response.msg == 201){
                  this.$modal.msgError("本月计划已设置，请勿重复提交！");
                }else {
                  this.$modal.msgSuccess("新增成功");
                }
                this.open = false;
                const planType = 1;
                this.getList(planType);
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id;
        this.$modal.confirm('是否确认删除该用药计划？').then(function() {
          return delOnePaln(ids);
        }).then(() => {
          const planType = 1;
          this.getList(planType);
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      getNurseElderList(){
        this.queryElder.userId = this.$store.state.user.id;
        nurseElderList(this.queryElder).then(response=>{
          this.elderList = response.rows;
          this.total = response.total;
        })
      },
      setMedicatePaln(item){
        this.elderInfo = false;
        this.elderPlan = true;
        this.queryParams.elderId = item.elderId;
        const planType = 1;
        this.getList(planType);
        this.eldertitle = "住户"+item.elderName+"的吃药计划表";
        this.elder = item;
      },
      goBack(){
        this.elderInfo = true;
        this.elderPlan = false;
      },
      handleClick(tab, event) {
        console.log(tab, event);
        console.log(tab.index)
        if(tab.index == 1){
          const planType = 2;
          this.getList(planType);
        }else {
          const planType = 1;
          this.getList(planType);
        }
      },
    }
  };
</script>
<style scoped>
  .firstCard{
    background-color: #bde6f8;
  }
  .mealPlan{
    font-size: 10px;
    background-color: #e7f6e9;
    height:150px;
  }
  .plan-card-container{
    background-color: #77c1fd;
    margin-top:15px;
    border-radius: 10px;
  }
  .time {
    font-size: 13px;
    color: #999;
  }
  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }
  .button {
    padding: 0;
    float: right;
  }
  .image {
    width: 100%;
    display: block;
  }
  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both  }
  .e-info{
    font-size: 13px;
  }
  .card-container{
    margin-top:15px;
    width: 150px;
    height: 250px;
  }
  .planElderList{
    height: 800px;
  }
</style>
